package 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import ginger.utils.NetUtil;
	import interactive.turntable.Turntable;
	import ginger.ui.simple.SimpleBrightnessButton;
	import ginger.utils.DisplayUtil;
	import UILib.ArrowBmpdata;
	import UILib.PanelBmpdata;
	import UILib.PlayBtnBmpdata;
	import flash.external.ExternalInterface;

	/**
	 * ...
	 * @author Ning
	 */
	[Frame(factoryClass="Preloader")]
	public class Main extends Sprite 
	{
        private var _panel:Bitmap;
		private var _playBtn:SimpleBrightnessButton;
		private var _table:Turntable;
		private var _angles:Array;
		private var _status:int;
		private var _result:int;
		private var _msg:String;
		public function Main():void 
		{
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			// entry point
			setTimeout(init, 200);
		}
		private function init():void
		{
			DisplayUtil.setStageTopLeftAndNoScale(this.stage);
			
			_angles = [];
			
			for (var i:int = 0; i < 8; i++ )
			{
				_angles.push(i*360 / 8-90);
			}
			
			
			_panel = new Bitmap(new PanelBmpdata);
			addChild(_panel);
			
			_table = new Turntable(20,-66,7000,.2,.1,5);
			addChild(_table);
			_table.x = 215;
			_table.y = 215;
			
			var arrowBmp:Bitmap = new Bitmap(new ArrowBmpdata);
			arrowBmp.smoothing = true;
			_table.addChild(arrowBmp);
			arrowBmp.y = -17;
			arrowBmp.x = 60;
			
			_playBtn = new SimpleBrightnessButton(new Bitmap(new PlayBtnBmpdata));
			addChild(_playBtn);
			_playBtn.x = 148;
			_playBtn.y = 146;
			
			_playBtn.addEventListener(MouseEvent.CLICK, onClick);
		}
		private function onClick(evt:MouseEvent):void
		{
			_playBtn.mouseEnabled = _playBtn.mouseChildren = false;
			_playBtn.removeEventListener(MouseEvent.CLICK, onClick);
			
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE, onDataComplete);
			urlloader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			var o:Object = NetUtil.getFlashVarsParam(this.stage, 'config');
			var url:String = o == null?'asset/data.xml':String(o) + Math.random();
			urlloader.load(new URLRequest(url));
		}
		private function onIOError(evt:IOErrorEvent=null):void
		{
			_status = -6;
			_msg = '服务器出错，请稍后再试';
			showresult();
		}
		private function onDataComplete(evt:Event):void
		{
			var data:XML;
			try {
				data = XML(evt.target.data);
			}
			catch (e:Error)
			{
				onIOError();
				return;
			}
			if (data.status == '') {
				onIOError();
				return;
			}
			
			_status= data.status;
			_result = data.result;
			_msg = data.msg;
			
			
			if (_result == 8)
			    _result = 0;
			
			if (_status == 0)
			{
				_table.resultAngle = _angles[_result];
				_table.play();
			    _table.addEventListener(Event.COMPLETE, onTableComplete);
			}
			else 
			{
				showresult();
			}
		}
		private function onTableComplete(evt:Event):void
		{
			showresult();
		}
		//结果展示
		private function showresult():void
		{
			_playBtn.mouseEnabled = _playBtn.mouseChildren = true;
			_playBtn.addEventListener(MouseEvent.CLICK, onClick);
			_table.removeEventListener(Event.COMPLETE, onTableComplete);
			
			trace(_status+' '+_msg)
			if (ExternalInterface.available)
			    ExternalInterface.call('showresult', _status, _msg);
		}
	}
}